package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxOrderStatusDao;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrderStatus;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderStatusService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxOrderStatusServiceImpl extends BaseService<BmsDxOrderStatus, Integer> implements BmsDxOrderStatusService
{

    @Autowired
    BmsDxOrderStatusDao bmsDxOrderStatusDao;

    @Override
    public BmsDxOrderStatusDao getBaseDao() {
        return bmsDxOrderStatusDao;
    }

    @Override
    public Integer getBusinessConsumerUsersNum(Integer businessId) {
        return bmsDxOrderStatusDao.countBusinessConsumerUsersNum(businessId);
    }

    @Override
    public List<Map> getBusinessConsumerUsers(Integer businessId, Pageable pageable) {
        return bmsDxOrderStatusDao.findBusinessConsumerUsers(businessId,pageable);
    }

    @Override
    public List<Map> getBusinessConsumerUsers(Integer businessId, Set userIds) {
        return bmsDxOrderStatusDao.findBusinessConsumerUsers(businessId,userIds);
    }

    @Override
    public List<Map> getBusinessConsumerUsers(Integer businessId) {
        return bmsDxOrderStatusDao.findBusinessConsumerUsers(businessId);
    }

    @Override
    public List<Map> getBusinessConsumerUsers(Set userIds) {
        return bmsDxOrderStatusDao.findBusinessConsumerUsers(userIds);
    }

    @Override
    public List<Map<String,Object>> countUserReceCoupon(Set userReceIds) {
        return bmsDxOrderStatusDao.countActivityOrderReceCoupon(userReceIds);
    }

    @Override
    public Map<Integer,Map<String,Object>> countCouponOrderSuccessNum(Integer buinessId,Set<Integer> userIds) {
        Map<Integer,Map<String,Object>> userConsumptionsCouponMap = new HashMap<>();
        List<Map> couponOrderMap = bmsDxOrderStatusDao.countCouponOrderSuccessNum(buinessId,userIds);
        if (couponOrderMap != null && couponOrderMap.size() > 0) {
            for (Map<String, Object> consumptionMap : couponOrderMap) {
                userConsumptionsCouponMap.put(Integer.parseInt(consumptionMap.get("userId").toString()),consumptionMap);
            }
        }
        return userConsumptionsCouponMap;
    }

    @Override
    public Map<Integer, Map<String, Object>> countGroupOrderSuccessNum(Integer businessId, Set<Integer> userIds) {
        Map<Integer,Map<String,Object>> userConsumptionsGroupCouponMap = new HashMap<>();
        List<Map> groupOrderMap = bmsDxOrderStatusDao.countGroupOrderSuccessNum(businessId,userIds);
        if (groupOrderMap != null && groupOrderMap.size() > 0) {
            for (Map<String, Object> gMap : groupOrderMap) {
                userConsumptionsGroupCouponMap.put(Integer.parseInt(gMap.get("userId").toString()),gMap);
            }
        }
        return userConsumptionsGroupCouponMap;
    }

    @Override
    public void deleteAllRefundStatus(Integer orderId,Integer status) {
        List<BmsDxOrderStatus> bmsDxOrderStatuses = bmsDxOrderStatusDao.findByOrderId(orderId);
        if(bmsDxOrderStatuses != null && bmsDxOrderStatuses.size() > 0){
            for(BmsDxOrderStatus bmsDxOrderStatus : bmsDxOrderStatuses){
                if(bmsDxOrderStatus.getStatus().intValue() == status.intValue()){
                    bmsDxOrderStatusDao.delete(bmsDxOrderStatus);
                }
            }
        }
    }

    public void deleteAllRefundStatus(Integer orderId,Set<Integer> status) {
        List<BmsDxOrderStatus> bmsDxOrderStatuses = bmsDxOrderStatusDao.findByOrderId(orderId);
        if(bmsDxOrderStatuses != null && bmsDxOrderStatuses.size() > 0){
            for(BmsDxOrderStatus bmsDxOrderStatus : bmsDxOrderStatuses){
                for(Integer st : status){
                    if(bmsDxOrderStatus.getStatus().intValue() == st.intValue()){
                        bmsDxOrderStatusDao.delete(bmsDxOrderStatus);
                    }
                }
            }
        }
    }

    @Override
    public void addOrderStatus(BmsDxOrder bmsDxOrder, Integer status) {
        BmsDxOrderStatus bmsDxOrderStatus = bmsDxOrderStatusDao.findByOrderIdAndStatus(bmsDxOrder.getId(),status);
        if(bmsDxOrderStatus == null) {
            bmsDxOrderStatus = new BmsDxOrderStatus();
            bmsDxOrderStatus.setOrderId(bmsDxOrder.getId());
            bmsDxOrderStatus.setOrderType(bmsDxOrder.getOrderType());
            bmsDxOrderStatus.setUserId(bmsDxOrder.getBmsUserId());
            bmsDxOrderStatus.setStatus(status);
            bmsDxOrderStatus.setNewTime(bmsDxOrder.getNewTime());
            bmsDxOrderStatus.setBusinessId(bmsDxOrder.getBmsBusinessId());
            bmsDxOrderStatus.setPayTime(bmsDxOrder.getPayTime());
            bmsDxOrderStatusDao.save(bmsDxOrderStatus);
        }
    }

    @Override
    public Map<Integer,Integer> countNoPayAndUse(Set<Integer> orderTypes, Integer userId) {
        Map<Integer,Integer> initCountMap = new HashMap<>();
        initCountMap.put(Constants.NO_PAY,0);
        initCountMap.put(Constants.ALREADY_USE,0);
        initCountMap.put(Constants.WAIT_GET,0);
        List<Map<String,Object>> countMap = bmsDxOrderStatusDao.countNoPayAndUse(orderTypes,userId);
        if(countMap != null && countMap.size() > 0){
            for(Map<String,Object> obj : countMap){
                if(initCountMap.containsKey(Integer.parseInt(obj.get("status").toString()))){
                    initCountMap.put(Integer.parseInt(obj.get("status").toString()),Integer.parseInt(obj.get("num").toString()));
                }
            }
        }
        return initCountMap;
    }

    @Override
    public List<BmsDxOrderStatus> findByBusinessId(Integer businessId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("businessId",businessId);
        return bmsDxOrderStatusDao.findAll(new SpecificationUtils(conditionMap));
    }
}
